<?php

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
    protected $table = 'admin_permissions';

    /**
     * [roles 权限和角色]
     * @return [type] [description]
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'admin_permission_role', 'permission_id', 'role_id');
    }

    /**
     * [users 权限和用户]
     * @return [type] [description]
     */
    public function users()
    {
        return $this->belongsToMany(AdminUser::class, 'admin_permission_user', 'permission_id', 'user_id');
    }

    /**
     * [getUserPermission 获得用户的所有权限]
     * @return [type] [description]
     */
    public function getUserPermission()
    {
        $UserPermission = Permission::join('admin_permission_user', 'admin_permission_user.permission_id', '=', 'admin_permissions.id')->select(['admin_permission_user.*', 'admin_permissions.*'])->get()->toArray();

        return $UserPermission;
    }
}
